package cn.shyouchi.wuguanjia.android.ui.event.labor;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.chart.onYVlaueChangeListner;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;

/**
 * Created by Eenie on 2016/11/3 at 10:45
 * Email: 472279981@qq.com
 * Des:
 */

public class PersonLaborAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public final static int ITEM_TYPE_PERSON_LABOR = 0;
    public final static int ITEM_EVENT_PERSON_RANK = 1;

    onYVlaueChangeListner mVlaueChangeListner;

    public PersonLaborAdapter(List<MultiItemEntity> data, onYVlaueChangeListner mVlaueChangeListner) {
        super(data);
        addItemType(ITEM_TYPE_PERSON_LABOR, R.layout.item_event_person_labor_layout);
        addItemType(ITEM_EVENT_PERSON_RANK, R.layout.item_event_person_rank_layout);
        this.mVlaueChangeListner = mVlaueChangeListner;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity entity) {
        switch (holder.getItemViewType()) {
            case ITEM_TYPE_PERSON_LABOR:
                final PersonEntity person = (PersonEntity) entity;


                ImageView imgState = holder.getView(R.id.img_item_expend_state);

                imgState.setImageResource(person.isExpanded() ? R.mipmap.ic_item_group_open : R.mipmap.ic_item_group_close);



                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (person.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                holder.setText(R.id.tv_person_name, person.getName());
                break;
            case ITEM_EVENT_PERSON_RANK:
                final PersonLaborEntity labor = (PersonLaborEntity) entity;

                holder.setText(R.id.tv_person_name, labor.getName());
                holder.setText(R.id.tv_count, String.format("%s天", labor.getCount()));
                holder.getView(R.id.tv_sub).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final PersonLaborEntity labor = (PersonLaborEntity) getData().get(holder.getPosition());
                        if (labor.getCount() > 0) {
                            labor.subCount();
                            holder.setText(R.id.tv_count, String.format("%s天", labor.getCount()));
                            mVlaueChangeListner.onChange(labor.getId(), (float) -1);
                        }
                    }
                });
                holder.getView(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final PersonLaborEntity labor = (PersonLaborEntity) getData().get(holder.getPosition());


                        PersonEntity p = (PersonEntity) labor.getTag();

                        LogUtil.e("count = " + p.getPersonLaborCount());

                        if (mVlaueChangeListner.isCanChange(labor.getId())) {
                            if (p.isCanAdd()) {
                                labor.addCount();
                                holder.setText(R.id.tv_count, String.format("%s天", labor.getCount()));
                                mVlaueChangeListner.onChange(labor.getId(), (float) 1);
                            } else {
                                ToastUtil.showToast("该员工工时达到考勤上限");
                            }
                        } else {
                            ToastUtil.showToast("该班次没有可分配工时");
                        }
                    }
                });
                break;
        }
    }
}
